import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/gt_popup_v3.dart';
import 'package:lib_uikit/widgets/gt_selector_v3.dart';
import 'package:lib_uikit/widgets/time_picker/default_style.dart';
import 'package:lib_uikit/widgets/time_picker/gt_picker_with_selector_v3.dart';
import 'package:lib_uikit/widgets/time_picker/model/date_item_model.dart';
import 'package:lib_uikit/widgets/time_picker/model/date_mode.dart';
import 'package:lib_uikit/widgets/time_picker/model/pduration.dart';
import 'package:lib_uikit/widgets/time_picker/model/suffix.dart';
import 'package:lib_uikit/widgets/time_picker/picker_style_v3.dart';
import 'package:lib_uikit/widgets/time_picker/route/date_picker_route.dart';
import 'package:lib_uikit/widgets/time_picker/route/date_picker_route_and_selector.dart';
import 'package:lib_uikit/widgets/time_picker/time_utils.dart';

/// [onChanged]   选择器发生变动
/// [onConfirm]   选择器提交
/// [pickerStyle] 样式
/// [suffix] 后缀
class GTPickerV3 {
  /// 时间选择器
  /// [Suffix] : 每列时间对应的单位  默认：中文常规  Suffix(years: '年',month: '月');
  /// [selectDate] : 初始化选中时间  默认现在
  ///    PDuration.now();
  ///    PDuration.parse(DateTime.parse('20210139'));
  ///    PDuration(year: 2020,month: 2);
  /// [maxDate] : 最大时间 用法同上
  ///     tip: 当只有单列数据，该限制不产生关联 只针对单列item限制，比如 maxDate>day = 3  minDate>day = 10,那么所有的月份都只显示3-10之间
  /// [minDate] : 最小时间 用法同上
  /// [mode] : 时间选择器所显示样式  16 种时间样式 默认：DateMode.YMD
  static void showDatePicker(
    BuildContext context, {
    DateMode mode = DateMode.YMD,
    PDuration? selectDate,
    PDuration? maxDate,
    PDuration? minDate,
    PDuration? startDate,
    PDuration? endDate,
    Suffix? suffix,
    PickerStyle? pickerStyle,
    //选择器日期改变监听
    DateCallback? onChanged,
    //选择单个日期回调
    DateCallback? onConfirm,
    //选择多个日期回调
    DatesCallback? onDatesConfirm,
    Function(bool isCancel)? onCancel,
    String? title,
    String? subTitle,
    bool overlapTabBar = false,
    String? confirmText,
    String? cancelText,
    String? startText,
    String? endText,
    bool? startTextSelected,
    bool? endTextSelected,
    bool? showTopView,
    bool? isDismissible,
  }) {
    if (pickerStyle == null) {
      pickerStyle = DefaultPickerStyle(title, subTitle, startText, endText, startTextSelected, endTextSelected);
    }
    if (pickerStyle.context == null) {
      pickerStyle.context = context;
    }

    if (selectDate == null) {
      if (startDate != null) {
        selectDate = startDate;
      } else {
        selectDate = PDuration.now();
      }
    }
    if (suffix == null) suffix = Suffix.normal();

    // 解析是否有对应数据
    DateItemModel dateItemModel = DateItemModel.parse(mode);

    if (maxDate == null) maxDate = PDuration(year: 2100);
    if (minDate == null) minDate = PDuration(year: 1900);

    if ((dateItemModel.day || dateItemModel.year)) {
      if (intEmpty(selectDate.year)) {
        print('picker  Tip >>> initDate未设置years，默认设置为now().year');
        selectDate.year = DateTime.now().year;
      }

      /// 如果有年item ，必须限制
      if (intEmpty(maxDate.year)) maxDate.year = 2100;
      if (intEmpty(minDate.year)) minDate.year = 1900;

      // print('longer >>> ${minDate.year}');

      if (dateItemModel.month || dateItemModel.day) {
        assert(minDate.year! > 1582, 'min Date Year must > 1582');
      }
    }

    Navigator.of(context, rootNavigator: overlapTabBar).push(DatePickerRoute(
      mode: mode,
      initDate: selectDate,
      maxDate: maxDate,
      minDate: minDate,
      startDate: startDate,
      endDate: endDate,
      suffix: suffix,
      pickerStyle: pickerStyle,
      onChanged: onChanged,
      onConfirm: onConfirm,
      onDatesConfirm: onDatesConfirm,
      onCancel: onCancel,
      theme: Theme.of(context),
      confirmText: confirmText,
      cancelText: cancelText,
      title: title,
      subTitle: subTitle,
      startText: startDate == null
          ? startText
          : "${startDate.year}-${TimeUtils.intToStr(startDate.month ?? 0)}-${TimeUtils.intToStr(startDate.day ?? 0)}",
      endText: endDate == null
          ? endText
          : "${endDate.year}-${TimeUtils.intToStr(endDate.month ?? 0)}-${TimeUtils.intToStr(endDate.day ?? 0)}",
      startTextSelected: startTextSelected,
      endTextSelected: endTextSelected,
      showTopView: showTopView,
      isDismissible: isDismissible,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
    ));
  }


  static void showDatePickerAndSelector(
      BuildContext context, {
        DateMode mode = DateMode.YMD,
        PDuration? selectDate,
        PDuration? maxDate,
        PDuration? minDate,
        PDuration? startDate,
        PDuration? endDate,
        Suffix? suffix,
        PickerStyle? pickerStyle,
        //选择器日期改变监听
        DateCallback? onChanged,
        //选择单个日期回调
        DateCallback? onConfirm,
        //选择多个日期回调
        DatesCallback? onDatesConfirm,
        String? title,
        String? subTitle,
        bool overlapTabBar = false,
        String? confirmText,
        String? cancelText,
        String? startText,
        String? endText,
        bool? startTextSelected,
        bool? endTextSelected,
        bool? showTopView,
        bool? isDismissible,
        bool? showSelector,
        List<GTSelectorModalV3>? list,
        PDuration Function(int index, PDuration endDate)? calculateStartTimeWhenClick,
        String? selectorTitle,
        int? selectIndex,
        Widget? selectorBottomWidget,
        VoidCallback? onCancelClick,
      }) {
    if (pickerStyle == null) {
      pickerStyle = DefaultPickerStyle(title, subTitle, startText, endText, startTextSelected, endTextSelected);
    }
    if (pickerStyle.context == null) {
      pickerStyle.context = context;
    }

    if (selectDate == null) {
      if (startDate != null) {
        selectDate = startDate;
      } else {
        selectDate = PDuration.now();
      }
    }
    if (suffix == null) suffix = Suffix.normal();

    // 解析是否有对应数据
    DateItemModel dateItemModel = DateItemModel.parse(mode);

    if (maxDate == null) maxDate = PDuration(year: 2100);
    if (minDate == null) minDate = PDuration(year: 1900);

    if ((dateItemModel.day || dateItemModel.year)) {
      if (intEmpty(selectDate.year)) {
        print('picker  Tip >>> initDate未设置years，默认设置为now().year');
        selectDate.year = DateTime.now().year;
      }

      /// 如果有年item ，必须限制
      if (intEmpty(maxDate.year)) maxDate.year = 2100;
      if (intEmpty(minDate.year)) minDate.year = 1900;

      // print('longer >>> ${minDate.year}');

      if (dateItemModel.month || dateItemModel.day) {
        assert(minDate.year! > 1582, 'min Date Year must > 1582');
      }
    }

    Navigator.of(context, rootNavigator: overlapTabBar).push(DatePickerRouteAndSelector(
      mode: mode,
      initDate: selectDate,
      maxDate: maxDate,
      minDate: minDate,
      startDate: startDate,
      endDate: endDate,
      suffix: suffix,
      pickerStyle: pickerStyle,
      onChanged: onChanged,
      onConfirm: onConfirm,
      onDatesConfirm: onDatesConfirm,
      theme: Theme.of(context),
      confirmText: confirmText,
      cancelText: cancelText,
      title: title,
      subTitle: subTitle,
      startText: startDate == null
          ? startText
          : "${startDate.year}-${TimeUtils.intToStr(startDate.month ?? 0)}-${TimeUtils.intToStr(startDate.day ?? 0)}",
      endText: endDate == null
          ? endText
          : "${endDate.year}-${TimeUtils.intToStr(endDate.month ?? 0)}-${TimeUtils.intToStr(endDate.day ?? 0)}",
      startTextSelected: startTextSelected,
      endTextSelected: endTextSelected,
      showTopView: showTopView,
      isDismissible: isDismissible,
      showSelector: showSelector,
      list: list,
      selectorTitle: selectorTitle,
      calculateStartTimeWhenClick: calculateStartTimeWhenClick,
      selectIndex: selectIndex,
      selectorBottomWidget: selectorBottomWidget,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      onCancelClick: onCancelClick,
    ));
  }

  static void showDatePickerWithSelector(
    BuildContext context,
    List<GTSelectorModalV3> list,
    PDuration startDate,
    PDuration endDate,
    PDuration Function(int index, PDuration endDate) getStartTimeWhenClick, {
    DateMode mode = DateMode.YMD,
    PDuration? maxDate,
    PDuration? minDate,
    PDuration? selectDate,
    String? title,
    String? subTitle,
    String? confirmText,
    String? cancelText,
    ValueChanged<int>? onSingleClick,
    VoidCallback? onConfirmClick,
    DatesCallback? onDatesConfirm,
    int? selectIndex,
    Widget? bottomWidget,
  }) {
    GTPopupV3.showPopup(
        context,
        title ?? "",
        datePickerWithSelectorWidget(subTitle, startDate, endDate, list, onSingleClick, getStartTimeWhenClick,
            confirmText ?? "", cancelText ?? "", onDatesConfirm, selectIndex, bottomWidget),
        cancelText ?? "",
        confirmText ?? "", onConfirmClick: () {
      onConfirmClick?.call();
    });
  }

  static Widget datePickerWithSelectorWidget(
    String? subTitle,
    PDuration startDate,
    PDuration endDate,
    List<GTSelectorModalV3> list,
    ValueChanged<int>? onSingleClick,
    PDuration Function(int index, PDuration endDate) calculateStartTimeWhenClick,
    String confirmText,
    String cancelText,
    DatesCallback? onDatesConfirm,
    int? selectIndex,
    Widget? bottomWidget,
  ) {
    return GTPickerWithSelectorV3(
      startDate: startDate,
      endDate: endDate,
      list: list,
      subTitle: subTitle,
      calculateStartTimeWhenClick: calculateStartTimeWhenClick,
      confirmText: confirmText,
      cancelText: cancelText,
      onSingleClick: (index) {
        onSingleClick?.call(index);
      },
      onDatesConfirm: (startDate, endDate) {
        if (onDatesConfirm != null) {
          onDatesConfirm(startDate, endDate);
        }
      },
      selectIndex: selectIndex,
      bottomWidget: bottomWidget,
    );
  }
}
